﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using NCI.EasyObjects;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using CMS.BLL.Entity;
using CMS.DAL;

namespace CMS.BLL
{
    public class Link
    {
         #region Common
        public eLink   Data;

        public Link()
        {
            Data = new eLink();
		}

        ~Link()
        {
		}

		public virtual void Dispose()
        {
		}

        public int DeleteById(int ID)
        {
            int nResult = 0;
            cLink objDAL = new cLink();
            try
            {
                objDAL.LoadByPrimaryKey(ID);
                objDAL.MarkAsDeleted();
                objDAL.Save();
                nResult = ID;
            }
            catch
            {
                nResult = 0;
            }
            return nResult;
        }

        public void LoadById(int ID)
        {
            cLink objDAL = new cLink();
            objDAL.LoadByPrimaryKey(ID);
            ConvertData(objDAL);
        }

        public int Insert()
        {
            int nResult = 0;
            cLink objInfor = new cLink(); 
            try
            {
                objInfor.AddNew();
                this.MappingData(objInfor);
                objInfor.Save();
                nResult = 1;
            }
            catch
            {
                nResult = 0; 
            }
            return nResult;
        }

        public int Update(int ID)
        {
            int nResult = 0;
            cLink objInfor = new cLink();
            try
            {
                objInfor.LoadByPrimaryKey(ID);
                this.MappingData(objInfor);
                objInfor.Save();
                nResult = ID; 
            }
            catch
            {
                nResult = 0; 
            }
            return nResult;
        }

        public int UpdateStatus(int ID, int Status)
        {
            int nResult = 0;
            cLink objDAL = new cLink();
            try
            {
                objDAL.LoadByPrimaryKey(ID);
                objDAL.Status = Status;
             
                objDAL.Save();
                nResult = objDAL.LinkID;
            }
            catch
            {
                nResult = 0;
            }
            return nResult;
        }

        private void MappingData(cLink objInfor)
        {
            objInfor.Name = Data.Name;
            objInfor.Description = Data.Description;
            objInfor.URL = Data.URL;
            objInfor.Status = Data.Status;
            objInfor.Priority = Data.Priority;
            objInfor.CreatedDate = Data.CreatedDate;  
         
            
        }

        private void ConvertData(cLink objInfor)
        {
            Data.Name = objInfor.Name;
            Data.Description = objInfor.Description;
            Data.URL = objInfor.URL;
            Data.Status = objInfor.Status;
            Data.Priority = objInfor.Priority;
            Data.CreatedDate = objInfor.CreatedDate;
         
        }
        #endregion

        public DataTable Search()
        {
            DataTable tbResult = new DataTable();
            cLink objDAL = new cLink();
            tbResult = objDAL.Search();
            return tbResult;
        }
    }
}
